package cn.com.self.apis.portal.service.impl;

import cn.com.self.apis.portal.entity.DatatypeEntity;
import cn.com.self.apis.portal.entity.DatatypeGroupEntity;
import cn.com.self.apis.portal.mapper.DatatypeGroupMapper;
import cn.com.self.apis.portal.mapper.DatatypeMapper;
import cn.com.self.apis.portal.service.intf.IDatatypeGroupService;
import cn.com.self.apis.portal.util.RedisCodeUtil;
import cn.com.self.apis.portal.util.RedisUtils;
import cn.com.selfsoft.smartcity.commons.exception.EntityNotFoundException;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 数据类型组 服务实现类
 */
@Service
public class DatatypeGroupServiceImpl extends BaseServiceImpl<DatatypeGroupEntity, String> implements IDatatypeGroupService {

    @Autowired
    private DatatypeMapper datatypeMapper;

    @Autowired
    private DatatypeGroupMapper datatypeGroupMapper;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void delete(long id) {
        //删除字典数据关联
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("group_id",id);
        this.datatypeMapper.deleteByMap(queryParams);
        DatatypeGroupEntity t = this.datatypeGroupMapper.selectById(id);
        if (null != t) {
            this.datatypeGroupMapper.deleteById(id);
        } else {
            throw new EntityNotFoundException("需要删除的资源不存在");
        }
    }

    @Override
    public Long countByGrouptype(String grouptype) {
        return this.datatypeGroupMapper.countByGrouptype(grouptype);
    }

    /**
     * 全局更新redis字典缓存
     */
    @Override
    public void initDatatypeRedis() {
        Sort sort = new Sort(Sort.Direction.DESC,"grouptype");
        Map map=new HashMap<String ,Object>() ;
        List<DatatypeGroupEntity> listG= this.list(map,sort);
        for(DatatypeGroupEntity g:listG){
            try {
                String grouptype=g.getGrouptype();
                List<DatatypeEntity> list=datatypeGroupMapper.getListBygroupIdOrgrouptype(null,grouptype);
                if(list !=null && list.size()>0){
                    redisUtils.lSet(RedisCodeUtil.list_datatype_+grouptype,list);
                    for(DatatypeEntity d:list){
                        redisUtils.set(grouptype+d.getCode(),d);
                        redisUtils.set(RedisCodeUtil.datatype_name+grouptype+d.getCode(),d.getName());
                    }
                }
            }catch (Exception e){
                System.out.println(e);
            }
        }
    }

    /**
     * 更新redis字典缓存
     */
    @Override
    public void updateRedis(DatatypeGroupEntity g){
        try {
            String grouptype=g.getGrouptype();
            List<DatatypeEntity> list=datatypeGroupMapper.getListBygroupIdOrgrouptype(null,grouptype);
            if(list !=null && list.size()>0){
                redisUtils.lSet(RedisCodeUtil.list_datatype_+grouptype,list);
            }
        }catch (Exception e){
            System.out.println(e);
        }

    }
}
